package com.car.loan.project.cm.service.impl;

import com.car.loan.project.cm.util.bo.BankSchemeUpdateInfo;
import com.car.loan.project.cm.util.bo.CmBankCarTypeUpdateBo;
import java.time.LocalDateTime;

import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmBankCarType;
import com.car.loan.project.cm.domain.CmBankScheme;
import com.car.loan.project.cm.mapper.CmBankCarTypeMapper;
import com.car.loan.project.cm.mapper.CmBankSchemeMapper;
import com.car.loan.project.cm.util.bo.BankSchemeInfo;
import com.car.loan.project.cm.util.bo.BankUpdateBo;
import com.car.loan.project.cm.util.bo.CmBankCarTypeBo;
import com.car.loan.project.cm.util.select.BankBaseDataSelect;
import com.car.loan.project.cm.util.vo.SelectedVo;
import java.util.ArrayList;
import java.util.Date;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.car.loan.common.utils.DateUtils;
import com.car.loan.project.cm.domain.CmBank;
import com.car.loan.project.cm.mapper.CmBankMapper;
import com.car.loan.project.cm.service.ICmBankService;
import com.car.loan.project.cm.util.bo.BankAddBo;
import java.util.Collection;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 按揭银行Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmBankServiceImpl implements ICmBankService {

    @Autowired
    private CmBankMapper cmBankMapper;

    @Autowired
    private CmBankSchemeMapper bankSchemeMapper;

    @Autowired
    private CmBankCarTypeMapper cmBankCarTypeMapper;

    /**
     * 查询按揭银行
     *
     * @param id 按揭银行ID
     * @return 按揭银行
     */
    @Override
    public CmBank selectCmBankById(Long id) {
        return cmBankMapper.selectById(id);
    }

    /**
     * 查询按揭银行列表
     *
     * @param cmBank 按揭银行
     * @return 按揭银行
     */
    @Override
    public List<CmBank> selectCmBankList(CmBank cmBank) {
        QueryWrapper<CmBank> queryWrapper = new QueryWrapper<>(cmBank);
        return cmBankMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CmBank> selectPage(IPage<CmBank> page, QueryWrapper<CmBank> queryWrapper) {
        return cmBankMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SelectedVo> selectedList(BankBaseDataSelect bankBaseDataSelect) {

        QueryWrapper<CmBank> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bankBaseDataSelect.getName())) {
            queryWrapper.like("name", bankBaseDataSelect.getName());
        }
        queryWrapper.select("id,alias,status");
        if (null == bankBaseDataSelect.getOf() || SwitchStatus.OPEN.status == bankBaseDataSelect
            .getOf()) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        List<CmBank> cmBanks = cmBankMapper.selectList(queryWrapper);
        List<SelectedVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(cmBanks)) {
            cmBanks.forEach(cmDealer -> {
                selectedVos.add(new SelectedVo().setValue(String.valueOf(cmDealer.getId()))
                    .setLabel(cmDealer.getAlias()).setStatus(cmDealer.getStatus()));
            });
        }
        return selectedVos;
    }

    /**
     * 新增按揭银行
     *
     * @param cmBank 按揭银行
     * @return 结果
     */
    @Override
    public int insertCmBank(CmBank cmBank) {
        cmBank.setCreateTime(DateUtils.getNowDate());
        return cmBankMapper.insert(cmBank);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertCmBank(BankAddBo bankAddBo) {
        Long loginUserId = SecurityUtils.getLoginUserId();
        CmBank cmBank = new CmBank();
        cmBank.setName(bankAddBo.getName());
        cmBank.setAlias(bankAddBo.getAlias());
        cmBank.setStatus(bankAddBo.getStatus());
        cmBank.setCreatorId(loginUserId);
        cmBank.setGracePeriod(bankAddBo.getGracePeriod());
        cmBank.setModifyTime(new Date());
        cmBank.setCreateTime(new Date());
        cmBank.setSettlement(bankAddBo.getSettlement());
        cmBank.setId(null);
        cmBankMapper.insert(cmBank);
        List<CmBankCarTypeBo> bankCarTypes = bankAddBo.getBankCarTypes();
        if (StringUtils.isNotEmpty(bankCarTypes)) {
            for (CmBankCarTypeBo cmBankCarTypeBo : bankCarTypes) {
                CmBankCarType cmBankCarType = new CmBankCarType();
                cmBankCarType.setBankId(cmBank.getId());
                cmBankCarType.setCarType(cmBankCarTypeBo.getCarType());
                cmBankCarType.setStatus(cmBankCarTypeBo.getStatus());
                cmBankCarType.setCreatorId(loginUserId);
                cmBankCarType.setCreateTime(LocalDateTime.now());
                cmBankCarType.setId(null);
                cmBankCarTypeMapper.insert(cmBankCarType);
                List<BankSchemeInfo> bankSchemeInfos = cmBankCarTypeBo
                    .getBankSchemeInfos();
                if (StringUtils.isNotEmpty(bankSchemeInfos)) {
                    for (BankSchemeInfo bankSchemeInfo : bankSchemeInfos) {
                        CmBankScheme cmBankScheme = new CmBankScheme();
                        cmBankScheme.setBankId(cmBank.getId());
                        cmBankScheme.setBankCarTypeId(cmBankCarType.getId());
                        cmBankScheme.setCarType(bankSchemeInfo.getCarType());
                        cmBankScheme.setTerms(bankSchemeInfo.getTerms());
                        cmBankScheme.setRates(bankSchemeInfo.getRates());
                        cmBankScheme.setStatus(bankSchemeInfo.getStatus());
                        cmBankScheme.setId(null);
                        bankSchemeMapper.insert(cmBankScheme);
                    }
                }
            }
        }
        return cmBank.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCmBank(BankUpdateBo bankUpdateBo) {
        CmBank cmBank = cmBankMapper.selectById(bankUpdateBo.getId());
        if (null == cmBank) {
            return;
        }
        cmBank.setName(bankUpdateBo.getName());
        cmBank.setAlias(bankUpdateBo.getAlias());
        cmBank.setStatus(bankUpdateBo.getStatus());
        cmBank.setModifyTime(new Date());
        cmBank.setGracePeriod(bankUpdateBo.getGracePeriod());
        cmBank.setSettlement(bankUpdateBo.getSettlement());
        cmBankMapper.updateById(cmBank);

        List<CmBankCarTypeUpdateBo> bankCarTypeUpdateBos = bankUpdateBo.getBankCarTypeUpdateBos();
        if (StringUtils.isNotEmpty(bankCarTypeUpdateBos)) {
            for (CmBankCarTypeUpdateBo bankCarTypeUpdateBo : bankCarTypeUpdateBos) {
                CmBankCarType cmBankCarType = new CmBankCarType();
                cmBankCarType.setBankId(cmBank.getId());
                cmBankCarType.setCarType(bankCarTypeUpdateBo.getCarType());
                cmBankCarType.setStatus(bankCarTypeUpdateBo.getStatus());
                cmBankCarType.setCreatorId(SecurityUtils.getLoginUserId());
                cmBankCarType.setCreateTime(LocalDateTime.now());
                cmBankCarType.setId(bankCarTypeUpdateBo.getId());
                if (null != cmBankCarType.getId() && cmBankCarType.getId() > 0) {
                    cmBankCarTypeMapper.updateById(cmBankCarType);
                } else {
                    cmBankCarTypeMapper.insert(cmBankCarType);
                }

                List<BankSchemeUpdateInfo> bankSchemeInfos = bankCarTypeUpdateBo
                    .getBankSchemeInfos();
                if (StringUtils.isNotEmpty(bankSchemeInfos)) {
                    for (BankSchemeUpdateInfo bankSchemeInfo : bankSchemeInfos) {
                        CmBankScheme cmBankScheme = new CmBankScheme();
                        cmBankScheme.setBankId(cmBank.getId());
                        cmBankScheme.setBankCarTypeId(cmBankCarType.getId());
                        cmBankScheme.setCarType(bankSchemeInfo.getCarType());
                        cmBankScheme.setTerms(bankSchemeInfo.getTerms());
                        cmBankScheme.setRates(bankSchemeInfo.getRates());
                        cmBankScheme.setStatus(bankSchemeInfo.getStatus());
                        cmBankScheme.setId(bankSchemeInfo.getId());
                        if (null != cmBankScheme.getId() && cmBankScheme.getId() > 0) {
                            bankSchemeMapper.updateById(cmBankScheme);
                        } else {
                            bankSchemeMapper.insert(cmBankScheme);
                        }
                    }
                }
            }
        }
    }

    /**
     * 修改按揭银行
     *
     * @param cmBank 按揭银行
     * @return 结果
     */
    @Override
    public int updateCmBank(CmBank cmBank) {
        return cmBankMapper.updateById(cmBank);
    }

    /**
     * 删除按揭银行对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmBankByIds(Collection<Long> ids) {
        return cmBankMapper.deleteBatchIds(ids);
    }

    /**
     * 删除按揭银行信息
     *
     * @param id 按揭银行ID
     * @return 结果
     */
    @Override
    public int deleteCmBankById(Long id) {
        return cmBankMapper.deleteById(id);
    }
}
